<template>
  <div class="app-container">
    <el-form :inline="true"  class="demo-form-inline">
        <el-form-item v-model="searchMap.where.name" label="模板名称">
            <el-input   placeholder="请输入模板名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="search()">搜索</el-button>
        </el-form-item>  
        <el-form-item>
            <el-button type="danger"  plain>批量删除</el-button>
        </el-form-item>   
        <el-button
            type="success"
            icon="el-icon-plus"
            @click="dialogFormVisible = true"
            >添加模板</el-button
            >
        
    </el-form>
    

    <!-- 数据表格 -->
        <el-table :data="tempList"  style="width: 100%"  >
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="模板名称">
        </el-table-column>
        <el-table-column
            prop="brands"
            label="模板关联品牌">
            <template slot-scope="scope">
                <span>{{scope.row.brands | strToJson()}}</span>
            </template>
        </el-table-column>
        <el-table-column
            prop="spec"
            label="关联的规格">
            <template slot-scope="scope">
                <span>{{scope.row.spec | strToJson()}}</span>
            </template>
        </el-table-column>
        <el-table-column width="200px" 
                fixed = "right"
                label="操作">
            <template slot-scope="scope">
                <el-button size="mini" type="success" icon="el-icon-edit" @click="updateSpecOptions(scope.row.id)">编辑</el-button>
                <el-button size="mini" type="danger" icon="el-icon-delete" @click="delSpecOptions(scope.row)" >删除</el-button>
            </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchMap.page"
            :page-sizes="[1, 3, 5, 10]"
            :page-size="searchMap.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>




    <!-- 添加模板 -->
  <el-dialog
    title="模板编辑"
    :visible.sync="dialogFormVisible"
    :close-on-click-modal="false"
  >
    <el-form>
      <el-form-item label="模板名称" label-width="100px">
        <el-input v-model="entity.name"></el-input>
      </el-form-item>
      <el-form-item label="关联品牌" label-width="100px">
          <el-select v-model="entity.selectbrands" multiple placeholder="请选择品牌">
            <el-option
            v-for="item in brands"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联规格" label-width="100px">
          <el-select v-model="entity.selectspec" multiple placeholder="请选择规格">
            <el-option
            v-for="item in specs"
            :key="item.id"
            :label="item.specName"
            :value="item.id">
            </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="saveTmp()">确 定</el-button>
    </div>
  </el-dialog>
  </div>

  
</template>


<script>
import {search,save} from '../api/temp'
import {findAllBrand} from '../api/brand'
import {findAllSpec} from '../api/spec'
export default {
  data() {
    return {
      dialogFormVisible: false,
      entity: {
           brands:[],
           spec:[]
      },
      brands:[], //品牌列表
      specs:[],//规格列表
      searchMap: {
        page: 1,
        size: 5,
        where: {
          name: "",
        },
      },
      tempList: [],
      total: 0,
    };
  },
  methods: {
    // 查询所有的品牌
    findAllBrand(){
        findAllBrand().then(res =>{
            this.brands = res.data;
        })
    },
    // 查询所有的规格
    findAllSpec(){
        findAllSpec().then(res =>{
            this.specs = res.data;
        })
    },
    //提交保存
    saveTmp(){
        //数据处理
        // 处理品牌
        var chooseBrandsArr = [];
        var chooseSpecsArr = [];
        this.brands.forEach(brand =>{
            // 判断是不是用户勾选的
            if(this.entity.selectbrands.indexOf(brand.id) != -1){
                chooseBrandsArr.push({id:brand.id,name:brand.name});
            }
        })

        chooseBrandsArr = JSON.stringify(chooseBrandsArr);
        // 处理选项
        this.specs.forEach(spec =>{
            if(this.entity.selectspec.indexOf(spec.id) != -1){
                chooseSpecsArr.push({id:spec.id,specName:spec.specName})
            }
        })

        chooseSpecsArr = JSON.stringify(chooseSpecsArr);
        this.entity.brands = chooseBrandsArr;
        this.entity.spec = chooseSpecsArr;
        console.log(this.entity);

        save(this.entity).then(res =>{
             if(res.success){
                this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success'
                });
                this.dialogFormVisible = false;
                this.search();
              }else{
                this.$notify({
                title: '保存失败',
                message: res.msg,
                type: 'error'
                });
              }
        })
        this.dialogFormVisible = false;
    },


    search(){
        search(this.searchMap).then(res => {
            if(res.success){
                this.tempList = res.data;
                this.total = res.total;
            }
        })
    },
       //size变化的时候触发
    handleSizeChange(size) {
        this.searchMap.size = size;
        this.search();
    },
    //当前页发生变化的时候触发
    handleCurrentChange(page) {
        this.searchMap.page = page;
        this.search();
    },
  },
  filters:{
      strToJson(source){
        //   console.log(source);
          var objArr = JSON.parse(source);
          var names = [];
          objArr.forEach(obj => {
              if(obj.name){
                  names.push(obj.name)
              }
              if(obj.specName){
                  names.push(obj.specName)
              }
          });
          var namesStr = names.join(",")
          return namesStr;
      }
  },

  created(){
      this.search();
      this.findAllBrand();
      this.findAllSpec();
  }
};
</script>